<?php
include_once("../info_struct.php");
include_once("../Process_Data/process_data.php");
include_once("../LDB_API/input.php");
include_once("../Wizard/wizard_funcs.php");

set_time_limit(1000);

$reader = new XMLReader();
$ref = new infos();
if (!$reader->open('refs.xml'))
{
	die("Failed to open 'refs.xml'"); 
}

while ($reader->read())
{
	$info = new infos();
	if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'Ref')
	{
		while (!($reader->nodeType == XMLReader::END_ELEMENT && $reader->name == 'Ref'))
		{
			//get ID
			if ($reader->name=='Ref')
			{
				$info->id = $reader->getAttribute('id');
				$info->id = (int)substr($info->id, 4);
			}
			//get authors
			if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'authors')
			{
				$info->author = $reader->readString();
			}
			//get title
			if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'title')
			{
				$info->title = $reader->readString();
			}
			//get rest, year and pages
			if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'rest')
			{
				$info->other = $reader->readString();
				
				//get year
				$year = array();
				preg_match('#\(([0-9]{4})\)#', $info->other, $year);
				
				if (count($year) == 2)
				{
					$info->year = $year[1];
				}else
				{
					preg_match_all('#([0-9]{4})#', $info->other, $year);
					if (count($year[1]) == 1)
					{
						$info->year = $year[1][0];
					}else
					{
						for ($i=0; $i<count($year[1]); $i++)
						{
							if ($year[1][$i] <= 2014 && $year[1][$i] >= 1900)
							{
								$info->year = $year[1][$i];
							}
						}
					}
				}
				
				//get pages
				$pages = array();
				preg_match('#\s([0-9]+)[-]+([0-9]+)[\s|$]+#', $info->other, $pages);
				
				if (count($pages) > 0)
				{
					$info->pages = $pages[1]."-".$pages[2];
				}
			}
			
			//get zmath ID
			if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'link')
			{
				$info->zmath = $reader->getAttribute('zm');
			}
			
			//get DOI
			if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'doi')
			{
				$info->doi = $reader->readString();
			}
			
			//get note
			if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'note')
			{
				$info->note = $reader->readInnerXML();
			}
			
			$reader->read();
		}
			
		//processData
		
		if ($info->zmath)
		{
			$ref = process_zmath($info);
		}else if ($info->doi && !($info->zmath))
		{
			$ref = process_doi($info);
		}else
		{
			$ref = process_manual_data($info);			
		}
		
		$zm_rating = 100;
		$doi_rating = 100;
		$arx_rating = 100;
		$zm = new infos();
		$doi = new infos();
		$arx = new infos();
		$final = new infos();
		for ($i=0; $i < count($ref); $i++)
		{
			if ($ref[$i]->from == "zma" && ($ref[$i]->rating < $zm_rating || $ref[$i]->rating == ""))
			{
				$zm_rating = $ref[$i]->rating;
				$zm = $ref[$i];
			}
			if ($ref[$i]->from == "doi" && ($ref[$i]->rating < $doi_rating || $ref[$i]->rating == ""))
			{
				$doi_rating = $ref[$i]->rating;
				$doi = $ref[$i];
			}
			if ($ref[$i]->from == "arx" && ($ref[$i]->rating < $arx_rating || $ref[$i]->rating == ""))
			{
				$arx_rating = $ref[$i]->rating;
				$arx = $ref[$i];
			}
		}
		
		if ($zm)
		{
			$final = merge_infos($info, $zm);
		}elseif ($doi)
		{
			$final = merge_infos($final, $doi);
		}elseif ($arx)
		{
			$final = merge_infos($final, $arx);
		}
		if ($final)
		{
			add_ref_xml($final);
		}else
		{
			add_ref_xml($info);
		}
		
		unset($info);
	}
}
?>
